package com.ethannhu.todaynews.component.articledetail

import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.QuickAdapterHelper
import com.ethannhu.todaynews.R
import com.ethannhu.todaynews.activity.BaseViewModelActivity
import com.ethannhu.todaynews.component.content.Content
import com.ethannhu.todaynews.component.userdetail.UserDetailActivity
import com.ethannhu.todaynews.databinding.ActivityArticleDetailBinding
import com.ethannhu.todaynews.util.Constant
import com.ethannhu.todaynews.util.ImageUtil
import kotlinx.coroutines.launch

/*
 * 文章详情界面
 */
class ArticleDetailActivity : BaseViewModelActivity<ActivityArticleDetailBinding>() {
    private lateinit var helper: QuickAdapterHelper
    private lateinit var layoutManager: LinearLayoutManager
    private lateinit var viewModel: ArticleDetailViewModel
    private lateinit var adapter: CommentAdapter
    private lateinit var articleDetailHeaderAdapter: ArticleDetailHeaderAdapter

    override fun initViews() {
        super.initViews()
        layoutManager = LinearLayoutManager(this)
        binding.list.layoutManager = layoutManager
    }

    override fun initData() {
        super.initData()
        //内容VM的工厂类 ，因为默认创建 VM 会使用无参构造器，如果VM添加参数则需要这个类
        val articleDetailViewModelFactory =
            ArticleDetailViewModelFactory(intent.getStringExtra(Constant.ID)!!)
        viewModel = ViewModelProvider(this, articleDetailViewModelFactory)[ArticleDetailViewModel::class.java]
        initViewModel(viewModel)

        //适配器
        adapter = CommentAdapter(viewModel)
        helper = QuickAdapterHelper.Builder(adapter).build()

        //头部
        articleDetailHeaderAdapter = ArticleDetailHeaderAdapter().apply {
            addOnItemChildClickListener(R.id.user_container) { adapter, _, position ->
                startActivityExtraId(UserDetailActivity::class.java, viewModel.content.user!!.id!!)
            }
            setOnItemClickListener { _, _, _ ->


            }
        }
        helper.addBeforeAdapter(articleDetailHeaderAdapter)

        binding.list.adapter = helper.adapter

        lifecycleScope.launch {
            viewModel.data.collect {
                showData(it)
            }
        }

        //评论
        lifecycleScope.launch {
            viewModel.comments
                .collect { data ->
                    if (data.page == 1) {
                        adapter.submitList(data.data)
                    } else {
                        adapter.addAll(data.data!!)
                    }
                }
        }
        viewModel.loadData()
    }

    override fun initListeners() {
        super.initListeners()

        //返回按钮点击
        binding.back.setOnClickListener {
            finish()
        }

        //列表滚动
        binding.list.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                //获取第一个可见的item的 position
                val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()

                //获取 RecycleView距离顶部的总滚动距离
                val scrollY = recyclerView.computeVerticalScrollOffset()

                binding.userContainer.visibility = if (scrollY >= 350) View.VISIBLE else View.INVISIBLE

            }

        })
    }

    private fun showData(data: Content) {
        articleDetailHeaderAdapter.setItem(data, null)

        //用户信息
        ImageUtil.showAvatar(binding.icon, data.user!!.icon)
        binding.nickname.text = data.user!!.nickname

        if (data.isLike()) {
            binding.like.setImageResource(R.drawable.thumb_selected)
        } else {
            binding.like.setImageResource(R.drawable.baseline_thumb)
        }
    }
}